import { getMessage } from "../api/index.js"
import delay from "./delay.js";

export const gptReply = async (content, room, contact, type = 'room') => {
    try {
        let timmer, interval
        interval = 0
        if (room) {

            const pushRoom = (askObj, ansObj, res) => {
                console.log(res)
                let list = room.messageList || []

                if (list.length >= 10) {
                    list.shift()
                    list.shift()
                }

                list.push(askObj)
                list.push(ansObj)
                console.log(list)
                room.messageList = list
            }
            const messageList = JSON.parse(JSON.stringify(room.messageList || []))
            // console.log(cache)
            // let askObj = {"role": "user","content":content}
            // messageList.push(askObj)



            timmer = timmer ? clearInterval(timmer) : setInterval(() => {
                interval++
                if (interval == 15) {
                    room.say`${contact} 我想想啊，先别急，ᕦ(･ㅂ･)ᕤ`
                } else if ((interval > 20 && interval % 20 == 0)) {
                    room.say('-------ᕦ(･ㅂ･)ᕤ----emmmm-----')
                }
            }, 1000);
        }
        const params = { msg: content }
        const res = await getMessage(params)
        await delay(100);
        let says = res.data.result
        if (timmer) {
            clearInterval(timmer)
            timmer = null
        }

        if (room && contact && type == 'room') {

            room.say`${contact} ${says}`;
            let ansObj = { "role": "assistant", "content": says }
            // pushRoom(askObj,ansObj,res)

            return says
        } else {
            let ansObj = { "role": "assistant", "content": says }
            // pushRoom(askObj,ansObj,res)

            return says
        }
    } catch (e) {
        room.say`你说的我不理解,要不你再说一次？`
        console.log(e)
        return e
    }
}




